/*
 * Copyright (c) 2019.  鼎捷软件股份有限公司.All rights reserved.
 */

package com.digiwin.dap.middleware.util;

import com.digiwin.dap.middleware.exception.OperateException;

import java.io.UnsupportedEncodingException;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * hash算法帮助类
 */
public final class HashUtils {

    private final static String ALGORITHM_MD5 = "MD5";
    private final static String ALGORITHM_SHA1 = "SHA1";
    private final static String CHARACTOR_ENCODING = "UTF-8";

    public static int fnvhash(String data) {
        final int p = 16777619;
        int hash = (int) 2166136261L;
        for (int i = 0; i < data.length(); i++) {
            hash = (hash ^ data.charAt(i)) * p;
        }
        hash += hash << 13;
        hash ^= hash >> 7;
        hash += hash << 3;
        hash ^= hash >> 17;
        hash += hash << 5;
        return hash;
    }

    /**
     * 计算字符串的hash值
     *
     * @param string 明文
     * @return 哈希值
     */
    public static String hash(String string) {
        if (string.isEmpty()) {
            return "";
        }
        MessageDigest hash = null;
        try {
            hash = MessageDigest.getInstance(ALGORITHM_SHA1);
            byte[] bytes = hash.digest(string.getBytes(CHARACTOR_ENCODING));
            String result = "";
            for (byte b : bytes) {
                String temp = Integer.toHexString(b & 0xff);
                if (temp.length() == 1) {
                    temp = "0" + temp;
                }
                result += temp;
            }
            return result;
        } catch (NoSuchAlgorithmException | UnsupportedEncodingException e) {
            e.printStackTrace();
            throw new OperateException(e);
        }
    }


    /**
     * 获取对象的属性和属性值并以字符串方式返回
     *
     * @param object 对象
     * @return 属性和属性值组成的字符串
     */
    public static String objectToString(Object object) {

        List<Field> fieldList = new ArrayList<>();
        Class tempClass = object.getClass();
        //当父类为null的时候说明到达了最上层的父类(Object类).
        while (tempClass != null) {
            fieldList.addAll(Arrays.asList(tempClass.getDeclaredFields()));
            //得到父类,然后赋给自己
            tempClass = tempClass.getSuperclass();
        }

        StringBuilder result = new StringBuilder();
        result.append("{");
        try {
            // 遍历所有属性
            for (int j = 0; j < fieldList.size(); j++) {
                // 获取属性的名字
                String name = fieldList.get(j).getName();
                // 将属性的首字符大写，方便构造get，set方法
                name = name.substring(0, 1).toUpperCase() + name.substring(1);
                // 获取属性的类型
                String type = fieldList.get(j).getGenericType().toString();
                Method m;
                if ("boolean".equals(type)) {
                    if (name.contains("Is")) {
                        m = object.getClass().getMethod(fieldList.get(j).getName());
                    } else {
                        m = object.getClass().getMethod("is" + name);
                    }
                } else {
                    m = object.getClass().getMethod("get" + name);
                }
                Object value = m.invoke(object);
                if (value != null) {
                    if (j != fieldList.size() - 1) {
                        result.append(name + ":" + value + ",");
                    } else {
                        result.append(name + ":" + value);
                    }
                }
            }
            result.append("}");
            return result.toString();
        } catch (Exception e) {
            e.printStackTrace();
            throw new OperateException(e);
        }
    }

}
